﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;


namespace weikebuluo.wxgzpt.WeChat.VIP
{
    public partial class guest : System.Web.UI.Page
    {
        C_BlackList_DAL cBlackListDAL = new C_BlackList_DAL();
        C_Guestbook_DAL cGuestbookDAL = new C_Guestbook_DAL();
        C_Guestbook_Model cGuestModel = new C_Guestbook_Model();
        JsonOperate JsonOperate = new JsonOperate();
        DAL.C_GuestbookSet_DAL GuestbookSetDAL = new DAL.C_GuestbookSet_DAL();
        Model.C_GuestbookSet_Model GuestbookSetModel = new Model.C_GuestbookSet_Model();


        public string GuestbookSet_TopPic = "/Content/Images/WeChat/VIP/mpic.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {

            DataTable dtGuestBook = GuestbookSetDAL.GetList("User_ID=" + Request.QueryString["uid"].ToString()).Tables[0];
            GuestbookSet_TopPic = dtGuestBook.Rows[0]["GuestbookSet_TopPic"].ToString();

            switch (Request.QueryString["action"])
            {
                case "bindguestbook":
                    GetGuestbookList();
                    break;
                case "setly":
                    SubmitGuest();
                    break;
                case "resetly":
                    SubmitReGuest();
                    break;
            }
        }




        #region 查询留言的会员列表
        /// <summary>
        ///查询留言的会员列表
        /// </summary>
        public void GetGuestbookList()
        {
            string User_ID = Request.QueryString["uid"].ToString();

            DataTable dtGuestbook = cGuestbookDAL.GetList("User_ID = " + User_ID + " AND Guestbook_Stutas = 1  order by Guestbook_Layer desc ").Tables[0];//审核通过的留言
            Response.Write(JsonOperate.TableToBasicJson(dtGuestbook));
            Response.End();
        }
        #endregion

        #region 提交留言
        public void SubmitGuest() {
 

            string Guestbook_WeChatID = Request.QueryString["wxid"].ToString();
            string uid = Request.QueryString["uid"].ToString();

            DataTable dtBlackList = cBlackListDAL.GetList("BlackList_WeChatID ='" + Guestbook_WeChatID + "'").Tables[0];
            string Layer = cGuestbookDAL.GetRecordCount("Guestbook_Father = 0").ToString();
            if (dtBlackList.Rows.Count == 0)
            {
                cGuestModel.Guestbook_Name = Request.Form["nickname"].Trim().ToString();
                cGuestModel.Guestbook_Content = Request.Form["info"].Trim().ToString();
                cGuestModel.Guestbook_Time = DateTime.Now;
                cGuestModel.Guestbook_WeChatID = Guestbook_WeChatID;
                cGuestModel.Guestbook_Father = 0;
                cGuestModel.Guestbook_Stutas = 1;
                cGuestModel.Guestbook_Layer = (Layer + "1000").PadLeft(10,'0');
                cGuestModel.User_ID = int.Parse(uid);
                int re = cGuestbookDAL.Add(cGuestModel);
                if (re > 0)
                {
                    Response.Write("200");
                }
            }
            else {
                Response.Write("0");
            }
            Response.End();
        }
        #endregion


        #region 提交回复
        public void SubmitReGuest() {
            string Guestbook_WeChatID = Request.QueryString["wxid"].ToString();
            string uid = Request.QueryString["uid"].ToString();
            DataTable dtBlackList = cBlackListDAL.GetList("BlackList_WeChatID ='" + Guestbook_WeChatID + "'").Tables[0];
            if (dtBlackList.Rows.Count == 0)
            {
                DataTable dtfatherlayer = cGuestbookDAL.GetList("Guestbook_ID=" + int.Parse(Request.Form["fid"].Trim().ToString())).Tables[0];
                cGuestModel.Guestbook_Name = "回复:" + dtfatherlayer.Rows[0]["Guestbook_Name"];
                cGuestModel.Guestbook_Content = Request.Form["info"].Trim().ToString();
                cGuestModel.Guestbook_Time = DateTime.Now;
                cGuestModel.Guestbook_WeChatID = Guestbook_WeChatID;
                cGuestModel.Guestbook_Father = int.Parse(Request.Form["fid"].Trim().ToString());
                cGuestModel.Guestbook_Stutas = 1;
                if (int.Parse(dtfatherlayer.Rows[0]["Guestbook_Layer"].ToString()) > 0 )
                {
                    cGuestModel.Guestbook_Layer = (int.Parse(dtfatherlayer.Rows[0]["Guestbook_Layer"].ToString()) - 1).ToString().PadLeft(10, '0');
                }
                cGuestModel.User_ID = int.Parse(uid);
                int re = cGuestbookDAL.Add(cGuestModel);
                if (re > 0)
                {
                    Response.Write("200");
                }
            }
            else
            {
                Response.Write("0");
            }
            Response.End();
        }
        #endregion

    }
}